--[[
Baggins_Professions

Author: Daphne Pfister <tiiroib02@sneakemail.com>

Thanks to Nargiddley for writing Baggins

License

Copyright (c) 2007 Daphne Pfister

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


TODO:
Localize

]]

Baggins_Professions = AceLibrary("AceAddon-2.0"):new("AceConsole-2.0","AceEvent-2.0")

local Baggins_Professions = Baggins_Professions

local dewdrop = AceLibrary:HasInstance("Dewdrop-2.0") and AceLibrary("Dewdrop-2.0")
local BTS = AceLibrary:HasInstance("Babble-Tradeskill-2.2") and AceLibrary("Babble-Tradeskill-2.2")
local pt = AceLibrary("LibPeriodicTable-3.1")

local TypeLogic = {
	Crafted = {
		Name = "Items Craftable by Known Professions", -- Even if we can not make yet
		NameOther = "Items Craftable by Unknown Professions",
		Description = "Crafted Items",
		Everything = "Tradeskill.Crafted",
		Skills = {
			Alchemy =			"Tradeskill.Crafted.Alchemy",
			Armorsmith =			"Tradeskill.Crafted.Blacksmithing.Armorsmith",
			Blacksmithing =			"Tradeskill.Crafted.Blacksmithing.Basic",
			Cooking =			"Tradeskill.Crafted.Cooking",
			["Dragonscale Leatherworking"] ="Tradeskill.Crafted.Leatherworking.Dragonscale",
			["Elemental Leatherworking"] =	"Tradeskill.Crafted.Leatherworking.Elemental",
			Enchanting =			"Tradeskill.Crafted.Enchanting",
			Engineering =			"Tradeskill.Crafted.Engineering.Basic",
			["First Aid"] =			"Tradeskill.Crafted.First Aid",
			["Gnomish Engineering"] =	"Tradeskill.Crafted.Engineering.Gnomish",
			["Goblin Engineering"] =	"Tradeskill.Crafted.Engineering.Goblin",
			Jewelcrafting =			"Tradeskill.Crafted.Jewelcrafting",
			Leatherworking =		"Tradeskill.Crafted.Leatherworking.Basic",
			["Master Axesmith"] =		"Tradeskill.Crafted.Blacksmithing.Weaponsmith.Axesmith",
			["Master Hammersmith"] =	"Tradeskill.Crafted.Blacksmithing.Weaponsmith.Hammersmith",
			["Master Swordsmith"] =		"Tradeskill.Crafted.Blacksmithing.Weaponsmith.Swordsmith",
			["Mooncloth Tailoring"] =	"Tradeskill.Crafted.Tailoring.Mooncloth",
			Smelting =			"Tradeskill.Crafted.Smelting",
			Poisons =			"Tradeskill.Crafted.Poisons",
			-- ["Potion Master"] = 		"??",
			["Shadoweave Tailoring"] =	"Tradeskill.Crafted.Tailoring.Shadoweave",
			["Spellfire Tailoring"] =	"Tradeskill.Crafted.Tailoring.Spellfire",
			Tailoring =			"Tradeskill.Crafted.Tailoring.Basic",
			-- ["Transmutation Master"] =	"??",
			["Tribal Leatherworking"] =	"Tradeskill.Crafted.Leatherworking.Tribal",
			Weaponsmith =			"Tradeskill.Crafted.Blacksmithing.Weaponsmith.Basic",
		},
	},
	Gather = {
		Name = "Items Gathered by Known Professions", -- Even if we can not gather yet
		NameOther = "Items Gathered by Unknown Professions",
		Description = "Gathered Items",
		Everything = "Tradeskill.Gather",
		Skills = {
			Disenchant =			"Tradeskill.Gather.Disenchant",
			Fishing =			"Tradeskill.Gather.Fishing",
			Herbalism =			"Tradeskill.Gather.Herbalism",
			Mining =			"Tradeskill.Gather.Mining",
			["Pick Pocket"] =		"Tradeskill.Gather.Pickpocketing",
			Prospecting =			"Tradeskill.Gather.Prospecting",
			Skinning =			"Tradeskill.Gather.Skinning",
		},
	},
	Mat = {
		Name = "Items Used by Known Professions", -- Even if we can not use yet
		NameOther = "Items Used by Unknown Professions",
		Description = "Trade Materials",
		Everything = "Tradeskill.Mat.ByProfession",
		Skills = {
			Alchemy =			"Tradeskill.Mat.ByProfession.Alchemy",
			Armorsmith =			"Tradeskill.Mat.ByProfession.Blacksmithing.Armorsmith",
			Blacksmithing =			"Tradeskill.Mat.ByProfession.Blacksmithing.Basic",
			Cooking =			"Tradeskill.Mat.ByProfession.Cooking",
			["Dragonscale Leatherworking"] ="Tradeskill.Mat.ByProfession.Leatherworking.Dragonscale",
			["Elemental Leatherworking"] =	"Tradeskill.Mat.ByProfession.Leatherworking.Elemental",
			Enchanting =			"Tradeskill.Mat.ByProfession.Enchanting",
			Engineering =			"Tradeskill.Mat.ByProfession.Engineering.Basic",
			["First Aid"] =			"Tradeskill.Mat.ByProfession.First Aid",
			["Gnomish Engineering"] =	"Tradeskill.Mat.ByProfession.Engineering.Gnomish",
			["Goblin Engineering"] =	"Tradeskill.Mat.ByProfession.Engineering.Goblin",
			Jewelcrafting =			"Tradeskill.Mat.ByProfession.Jewelcrafting",
			Leatherworking =		"Tradeskill.Mat.ByProfession.Leatherworking.Basic",
			["Master Axesmith"] =		"Tradeskill.Mat.ByProfession.Blacksmithing.Weaponsmith.Axesmith",
			["Master Hammersmith"] =	"Tradeskill.Mat.ByProfession.Blacksmithing.Weaponsmith.Hammersmith",
			["Master Swordsmith"] =		"Tradeskill.Mat.ByProfession.Blacksmithing.Weaponsmith.Swordsmith",
			["Mooncloth Tailoring"] =	"Tradeskill.Mat.ByProfession.Tailoring.Mooncloth",
			Smelting =			"Tradeskill.Mat.ByProfession.Smelting",
			Poisons =			"Tradeskill.Mat.ByProfession.Poisons",
			-- ["Potion Master"] = 		"??",
			["Shadoweave Tailoring"] =	"Tradeskill.Mat.ByProfession.Tailoring.Shadoweave",
			["Spellfire Tailoring"] =	"Tradeskill.Mat.ByProfession.Tailoring.Spellfire",
			Tailoring =			"Tradeskill.Mat.ByProfession.Tailoring.Basic",
			-- ["Transmutation Master"] =	"??",
			["Tribal Leatherworking"] =	"Tradeskill.Mat.ByProfession.Leatherworking.Tribal",
			Weaponsmith =			"Tradeskill.Mat.ByProfession.Blacksmithing.Weaponsmith.Basic",
		},
	},
	Tool = {
		Name = "Tools Used by Known Professions", -- Even if we can not use yet
		NameOther = "Tools Used by Unknown Professions",
		Description = "Professional Tools",
		Everything = "Tradeskill.Tool",
		Skills = {
			Alchemy =	"Tradeskill.Tool.Alchemy",
			Blacksmithing =	"Tradeskill.Tool.Blacksmithing",
			Cooking =	"Tradeskill.Tool.Cooking",
			Enchanting =	"Tradeskill.Tool.Enchanting",
			Engineering =	"Tradeskill.Tool.Engineering",
			Fishing =	"Tradeskill.Tool.Fishing",
			Jewelcrafting =	"Tradeskill.Tool.Jewelcrafting",
			Mining =	"Tradeskill.Tool.Mining",
			Skinning =	"Tradeskill.Tool.Skinning",
		},
	},
	Recipe = {
		Name = "Recipes Learnable by Known Professions", -- Even if we already know it
		NameOther = "Recipes for Unknown Professions",
		Description = "Recipes",
		Everything = "Tradeskill.Recipe", -- Might be better to use GetItemInfo for recipe
		Skills = {
			Alchemy =	"Tradeskill.Recipe.Alchemy",
			Blacksmithing =	"Tradeskill.Recipe.Blacksmithing",
			Cooking =	"Tradeskill.Recipe.Cooking",
			Enchanting =	"Tradeskill.Recipe.Enchanting",
			Engineering =	"Tradeskill.Recipe.Engineering",
			["First Aid"] =	"Tradeskill.Recipe.First Aid",
			Fishing =	"Tradeskill.Recipe.Fishing",
			Jewelcrafting =	"Tradeskill.Recipe.Jewelcrafting",
			Leatherworking ="Tradeskill.Recipe.Leatherworking",
			Tailoring =	"Tradeskill.Recipe.Tailoring",
		},
	},
}
local translate = {}
local BTSR = {}

for _,v in pairs(TypeLogic) do
	for k in pairs(v.Skills or {}) do
		if not translate[k] then
			translate[k] = BTS and BTS[k] or k
		end
	end
end
for k,v in pairs(translate) do
	BTSR[v] = k
end


local known_skills = {}

local function UpdateSkills()
	for k in pairs(known_skills) do
		known_skills[k] = nil
	end
	for skillIndex = 1, GetNumSkillLines() do
		local skillName, isHeader = GetSkillLineInfo(skillIndex)
		local skill = BTSR[skillName]
		if (isHeader == nil) and skill then known_skills[skill] = 1 end
	end
end

local function Matches(bag,slot,rule)
	if not rule.test then return end
	local typelogic = TypeLogic[rule.test]
	if not typelogic then return end

	local link = GetContainerItemLink(bag, slot)
	if not link then return end
	local itemid = link:match("item:(%d+)")
	itemid = tonumber(itemid)

	if not pt:ItemInSet(itemid,typelogic.Everything) then return end

	local match = nil

	for skill,set in pairs(typelogic.Skills) do
		if known_skills[skill] then
			if pt:ItemInSet(itemid,set) then return rule.other == nil end
		end
	end

	return rule.other ~= nil
end

local function GetName(rule)
	local typelogic = TypeLogic[rule.test or "???"]
	if not typelogic then return "Professions: ???" end

	if rule.other then
		return typelogic.NameOther
	else
		return typelogic.Name
	end
end

local function CleanRule(rule)
	rule.test = "Mat"
	rule.other = nil
	rule.alt = nil -- Future
end

local function setRuleType(rule, test)
	rule.test = test
	Baggins:OnRuleChanged()
end

local function setRuleKnown(rule)
	rule.other = nil
	Baggins:OnRuleChanged()
end

local function setRuleUnknown(rule)
	rule.other = true
	Baggins:OnRuleChanged()
end

local function DewDropOptions(rule, level, value)
	if level == 1 then
		dewdrop:AddLine("text", "Type", "isTitle", true)

		for test,v in pairs(TypeLogic) do
			dewdrop:AddLine('text', v.Description, "checked", rule.test == test, 'func',setRuleType,'arg1',rule,'arg2',test)
		end
		dewdrop:AddLine('text', "", 'isTitle', true)
		dewdrop:AddLine('text', "Known Professions", "checked", not rule.other, 'func',setRuleKnown,'arg1',rule)
		dewdrop:AddLine('text', "Unknown Professions", "checked", rule.other ~= nil, 'func',setRuleUnknown,'arg1',rule)
	end
end

function Baggins_Professions:OnInitialize()
	UpdateSkills()
	Baggins:AddCustomRule("Professions", {
		DisplayName = "Professions",
		Description = "Filter by items related to Known / Unknown Professions",
		Matches = Matches,
		GetName = GetName,
		CleanRule = CleanRule,
		DewDropOptions = DewDropOptions
	})
end

function Baggins_Professions:OnEnable()
	self:RegisterEvent('SKILL_LINES_CHANGED',UpdateSkills)

	-- Cause Baggins to update all the bags just in case.
	--Baggins:ForceFullUpdate()
end

